package com.usian.controller;

import com.usian.mapper.KeMapper;
import com.usian.mapper.XueMapper;
import com.usian.mapper.YuanMapper;
import com.usian.pojo.Ke;
import com.usian.pojo.Xue;
import net.minidev.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/ru")
public class RuController {

    @Autowired
    KeMapper keMapper;

    @Autowired
    XueMapper xueMapper;

    @RequestMapping("/import")
    public void impo(HttpServletRequest request, HttpServletResponse response, MultipartFile file)throws Exception{

        try {
            HSSFWorkbook workbook2 = new HSSFWorkbook(file.getInputStream());
            HSSFSheet sheet2 = workbook2.getSheet("学生");
            int lastRowNum2 = sheet2.getLastRowNum();
            Xue emp2 = new Xue();
            for (int i = 1; i <= lastRowNum2; i++) {
                HSSFRow row = sheet2.getRow(i);
                int num=1;
                HSSFCell cell = row.getCell(num++);
                String name = cell.getStringCellValue();
                Integer age = (int)row.getCell(num++).getNumericCellValue();
                String sex = row.getCell(num++).getStringCellValue();

                emp2.setName(name);
                emp2.setAge(age);
                emp2.setSex(sex);
                xueMapper.insert(emp2);
            }
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            HSSFSheet sheet = workbook.getSheet("课程");
            int lastRowNum = sheet.getLastRowNum();
            Ke emp = new Ke();
            for (int i = 1; i <= lastRowNum; i++) {
                HSSFRow row = sheet.getRow(i);
                int num=1;
                HSSFCell cell = row.getCell(num++);
                String name = cell.getStringCellValue();
                Integer shu = (int)row.getCell(num++).getNumericCellValue();
                emp.setName(name);
                emp.setShu(shu);
                keMapper.insert(emp);
            }






        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
